<template>
	<div class="app-container">
		<el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="90px" class="queryForms">
			<el-form-item label="返修任务单" prop="repairNumber">
				<el-input v-model="queryParams.repairNumber" placeholder="请输入返修任务单" clearable @keyup.enter.native="handleQuery" maxlength="20" show-word-limit />
			</el-form-item>
			<el-form-item label="编号" prop="serialNumber">
				<el-input v-model="queryParams.serialNumber" placeholder="请输入编号" clearable @keyup.enter.native="handleQuery" maxlength="20" show-word-limit />
			</el-form-item>

			<el-form-item label="接收人" prop="partRecipientName">
				<el-input v-model="queryParams.partRecipientName" placeholder="请输入接收人" clearable @keyup.enter.native="handleQuery" maxlength="20" show-word-limit />
			</el-form-item>
			<el-form-item label="接收日期" prop="partRecipientDate">
				<el-date-picker v-model="queryParams.partRecipientDate" type="date" value-format="yyyy-MM-dd" placeholder="请选择接收日期"> </el-date-picker>
			</el-form-item>

			<el-form-item label="零件组图号" prop="partFigure">
				<el-input v-model="queryParams.partFigure" placeholder="请输入零件组图号" clearable @keyup.enter.native="handleQuery" maxlength="20" show-word-limit />
			</el-form-item>
			<el-form-item label="外委日期" prop="outsourcingDate">
				<el-date-picker v-model="queryParams.outsourcingDate" type="date" value-format="yyyy-MM-dd" placeholder="请选择日期"> </el-date-picker>
			</el-form-item>
			<el-form-item label="状态" prop="registrationStatus" label-width="70px">
				<el-select v-model="queryParams.registrationStatus" placeholder="请选择">
					<el-option v-for="dict in dict.type.inspect_status" :key="dict.value" :label="dict.label" :value="dict.value"> </el-option>
				</el-select>
			</el-form-item>
			<el-form-item>
				<el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
				<el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
			</el-form-item>
		</el-form>
		<el-row :gutter="10" class="mb8">
			<el-col :span="1.5">
				<el-button type="primary" icon="el-icon-plus" size="mini" @click="handleSubmits" v-hasPermi="['taskFilling:coutsource:submit']">提交</el-button>
			</el-col>
			<right-toolbar @changeTableData="changeTableData" menuNames="coutsource74" :showSearch.sync="showSearch" @queryTable="handleQuery" :columns.sync="columns" perm="taskFilling:coutsource:index"></right-toolbar>
		</el-row>

		<div :class="sidebar.opened && eleTabTop <= 85 ? 'eleTab' : !sidebar.opened && eleTabTop <= 85 ? 'eleTabs' : ''" class="informationTab" ref="eleTab">
			<el-table
				@expand-change="handleExpandChange"
				:key="treetablekey"
				lazy
				:load="loadListDes"
				:row-class-name="row_class_name"
				@mousedown.native="mousedownTable"
				@row-click="row_click"
				@cell-mouse-enter="cell_mouse_enter"
				@cell-mouse-leave="cell_mouse_leave"
				@mouseup.native="mouseupTable"
				@mouseleave.native="mouseupTable"
				ref="multipleTable"
				v-loading="loading"
				:data="EntranceList"
				@selection-change="handleSelectionChange"
				row-key="id"
				:tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
				border
				height="100%">
				<el-table-column type="selection" width="55" align="center" />
				<el-table-column label="序号" align="center" type="index" width="55">
					           <template slot-scope="scope">
						               
						<div>{{ scope.row.sort }}</div>
						           </template
					>
					       
				</el-table-column>

				<!-- <el-table-column label="记录编号" prop="recordNumber" min-width="150" :show-overflow-tooltip="true" v-if="columns[0].visible" /> -->
				<el-table-column label="编号" prop="serialNumber" min-width="150" :show-overflow-tooltip="true" v-if="columns[0].visible" />
				<el-table-column label="外委日期" prop="outsourcingDate" min-width="150" :show-overflow-tooltip="true" v-if="columns[1].visible" />
				<!-- <el-table-column label="供方" prop="deviceTypeId" min-width="150" :show-overflow-tooltip="true" v-if="columns[3].visible" /> -->
				<!-- <el-table-column label="送检时间" prop="inspectDate" min-width="150" :show-overflow-tooltip="true" v-if="columns[4].visible" /> -->
				<el-table-column label="返修单号" prop="repairNumber" min-width="150" :show-overflow-tooltip="true" v-if="columns[2].visible" />
				<!-- <el-table-column label="生产批次" prop="production_batch" min-width="150" :show-overflow-tooltip="true" v-if="columns[6].visible" /> -->
				<el-table-column label="产品名称" prop="productName" min-width="150" :show-overflow-tooltip="true" v-if="columns[3].visible" />
				<el-table-column label="产品型号" prop="productModel" min-width="150" :show-overflow-tooltip="true" v-if="columns[4].visible" />
				<el-table-column label="零件组名称" prop="partName" min-width="150" :show-overflow-tooltip="true" v-if="columns[5].visible" />
				<el-table-column label="零件组图号" prop="partFigure" min-width="150" :show-overflow-tooltip="true" v-if="columns[6].visible" />
				<el-table-column label="数量" prop="quantityquantity" min-width="150" :show-overflow-tooltip="true" v-if="columns[7].visible" />
				<!-- <el-table-column label="接收人" prop="recipientName" min-width="150" :show-overflow-tooltip="true" v-if="columns[12].visible" />
				<el-table-column label="发放人" prop="issuerName" min-width="150" :show-overflow-tooltip="true" v-if="columns[13].visible" />
				<el-table-column label="发放日期" prop="issuerDate" min-width="150" :show-overflow-tooltip="true" v-if="columns[14].visible" />
				<el-table-column label="合格证接收人" prop="certificateRecipientName" min-width="150" :show-overflow-tooltip="true" v-if="columns[15].visible" />
				<el-table-column label="接收日期" prop="certificateRecipientDate" min-width="150" :show-overflow-tooltip="true" v-if="columns[16].visible" /> -->
				<el-table-column label="零件接收人" prop="partRecipientName" min-width="150" :show-overflow-tooltip="true" v-if="columns[8].visible" />
				<el-table-column label="接收日期" prop="partRecipientDate" min-width="150" :show-overflow-tooltip="true" v-if="columns[9].visible" />
				<el-table-column label="状态" align="center" prop="registrationStatus" min-width="120" :show-overflow-tooltip="true" v-if="columns[10].visible">
					<template slot-scope="scope">
						<!-- <dict-tag :options="dict.type.inspect_status" :value="scope.row.registrationStatus" /> -->
						<el-tag type="warning" v-if="scope.row.registrationStatus == '0' || scope.row.registrationStatus == '1' || scope.row.registrationStatus == '7'">{{ scope.row.registrationStatus == "0" ? (scope.row.submitStatus == "2" ? "待外送" : "待质检") : scope.row.registrationStatus == "1" ? "已入库" : "待返检" }}</el-tag>
						<el-tag type="success" v-if="scope.row.registrationStatus == '3' || scope.row.registrationStatus == '5' || scope.row.registrationStatus == '6'">{{ scope.row.registrationStatus == "3" ? "合格" : scope.row.registrationStatus == "5" ? "让步接收" : "已下发" }}</el-tag>
						<el-tag type="danger" v-if="scope.row.registrationStatus == '2' || scope.row.registrationStatus == '4'">{{ scope.row.registrationStatus == "2" ? "退货" : "不合格" }}</el-tag>
					</template>
				</el-table-column>
				<el-table-column label="审核状态" align="center" prop="submitStatus" min-width="150" :show-overflow-tooltip="true" v-if="columns[11].visible">
					<template slot-scope="scope">
						<div v-if="scope.row.submitStatus == '0' && scope.row.hasChildren">待提交</div>
						<div v-if="scope.row.submitStatus == '1' && scope.row.hasChildren">生产已提交</div>
						<div v-if="scope.row.submitStatus == '2' && scope.row.hasChildren">质量已确认</div>
						<div v-if="scope.row.submitStatus == '3' && scope.row.hasChildren">质量驳回</div>
						<div v-if="!scope.row.hasChildren"></div>
					</template>
				</el-table-column>
				<el-table-column label="缺件备注" prop="deficiencyRemark" min-width="150" :show-overflow-tooltip="true" v-if="columns[12].visible" />
				<el-table-column label="操作" class-name="small-padding fixed-width" width="200px" fixed="right">
					<template slot-scope="scope">
						<section v-if="scope.row.hasChildren">
							<el-button size="mini" type="text" icon="el-icon-circle-check" @click="handleSubmit(scope.row)" v-hasPermi="['taskFilling:coutsource:submit']" v-if="scope.row.submitStatus == '0' || scope.row.submitStatus == '3'">提交</el-button>
							<!-- <el-button size="mini" type="text" icon="el-icon-edit" @click="handleEdit(scope.row)" v-hasPermi="['taskFilling:coutsource:checked']" v-if="scope.row.submitStatus == '1'">审核</el-button> -->
							<el-button size="mini" type="text" icon="el-icon-wallet" @click="handleAnnex(scope.row)" v-hasPermi="['taskFilling:coutsource:annex']">附件</el-button>
							<el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['taskFilling:coutsource:remove']" v-if="scope.row.submitStatus == '0' || scope.row.submitStatus == '3'">删除</el-button>
						</section>
						<section v-else>
							<el-button size="mini" type="text" icon="el-icon-wallet" @click="handleAnnex2(scope.row)" v-hasPermi="['taskFilling:coutsource:annex2']">附件</el-button>
						</section>
					</template>
				</el-table-column>
			</el-table>
		</div>

		<pagination menuNames="Pagination77" v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />

		<!-- 送检 -->
		<el-dialog :title="title" :visible.sync="open" width="900px" append-to-body v-dialogDrag>
			<el-form ref="form" class="add-form2" :model="form" :rules="rules" label-width="120px" :disabled="disabled">
				<div class="add-mid-title">检验项目</div>
				<el-form-item label="镀层" prop="claddingMaterial">
					<el-radio v-model="form.claddingMaterial" label="0">合格</el-radio>
					<el-radio v-model="form.claddingMaterial" label="1">不合格</el-radio>
				</el-form-item>
				<el-form-item label="涂层" prop="coating">
					<el-radio v-model="form.coating" label="0">合格</el-radio>
					<el-radio v-model="form.coating" label="1">不合格</el-radio>
				</el-form-item>
				<el-form-item label="外观" prop="appearance">
					<el-radio v-model="form.appearance" label="0">合格</el-radio>
					<el-radio v-model="form.appearance" label="1">不合格</el-radio>
				</el-form-item>
				<el-form-item label="规格型号" prop="specificationsModel">
					<el-radio v-model="form.specificationsModel" label="0">一致</el-radio>
					<el-radio v-model="form.specificationsModel" label="1">不一致</el-radio>
				</el-form-item>
				<el-form-item label="数量" prop="quantity">
					<el-radio v-model="form.quantity" label="0">一致</el-radio>
					<el-radio v-model="form.quantity" label="1">不一致</el-radio>
				</el-form-item>
				<el-form-item label="合格证明" prop="certificate">
					<el-radio v-model="form.certificate" label="0">有</el-radio>
					<el-radio v-model="form.certificate" label="1">无</el-radio>
				</el-form-item>
				<el-form-item label="螺纹" prop="screwThread">
					<el-radio v-model="form.screwThread" label="0">合格</el-radio>
					<el-radio v-model="form.screwThread" label="1">不合格</el-radio>
				</el-form-item>
				<div class="add-mid-title">检验结果</div>
				<el-form-item label="检验方式" prop="inspectionMethod">
					<el-select v-model="form.inspectionMethod" placeholder="请选择">
						<el-option v-for="dict in dict.type.inspection_method" :key="dict.value" :label="dict.label" :value="dict.value"> </el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="检验方法" prop="testMethod">
					<el-select v-model="form.testMethod" placeholder="请选择">
						<el-option v-for="dict in dict.type.test_method" :key="dict.value" :label="dict.label" :value="dict.value"> </el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="检验员" prop="checkoutId">
					<el-select v-model="form.checkoutId" placeholder="请选择" @change="changeUser">
						<el-option v-for="item in userList" :key="item.userId" :label="item.nickName" :value="item.userId"> </el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="数量" prop="quantityquantity">
					<el-input-number v-model="form.quantityquantity" placeholder="请输入数量" :min="0" :max="99999999" :precision="0" />
				</el-form-item>
				<!-- <el-form-item label="生产日期/保质期" prop="producedDateSel" style="width: 100%">
					<el-select :multiple-limit="10" v-model="form.producedDateSel" multiple filterable allow-create default-first-option placeholder="请选择生产日期/保质期">
						<el-option v-for="item in producedDatearr" :key="item.value" :label="item.label" :value="item.value"> </el-option>
					</el-select>
				</el-form-item> -->
				<el-form-item label="不合格处置情况" prop="disposalSituation" style="width: 100%">
					<el-input type="textarea" v-model="form.disposalSituation" placeholder="请输入不合格处置情况" maxlength="100" show-word-limit />
				</el-form-item>
				<el-form-item label="备注" prop="remark" style="width: 100%">
					<el-input type="textarea" v-model="form.remark" placeholder="请输入备注" maxlength="100" show-word-limit />
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button type="primary" @click="submitForm(3)">合格</el-button>
				<el-button type="primary" @click="submitForm(4)">不合格</el-button>
				<el-button type="primary" @click="submitForm(5)">让步接收</el-button>
				<el-button @click="cancel">取消</el-button>
			</div>
		</el-dialog>
		<!-- 合格证 -->
		<el-dialog title="上传合格证" :visible.sync="certopen" width="900px" append-to-body v-dialogDrag>
			<el-form ref="certform" class="add-form1" :model="certform" :rules="certrules" label-width="140px">
				<el-form-item label="合格证证号" prop="certificateRecipientNumber">
					<el-input v-model="certform.certificateRecipientNumber" placeholder="请输入合格证证号" maxlength="20" show-word-limit />
				</el-form-item>
				<el-form-item label="合格证接受日期" prop="certificateRecipientDate">
					<el-date-picker v-model="certform.certificateRecipientDate" type="date" value-format="yyyy-MM-dd" placeholder="请选择合格证接受日期"> </el-date-picker>
				</el-form-item>
				<el-form-item label="上传合格证明">
					<FileUpload @input="getFiles" :value="uploadList"></FileUpload>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button type="primary" @click="submitCertForm">确 定</el-button>
				<el-button @click="certCancel">取 消</el-button>
			</div>
		</el-dialog>
		<!-- 下发 -->
		<!-- <el-dialog title="下发" :visible.sync="issueopen" width="900px" append-to-body v-dialogDrag>
			<el-form ref="issueform" class="add-form1" :model="issueform" :rules="issuerules" label-width="120px">
				<el-form-item label="零件接收人" prop="partRecipientId">
					<el-select v-model="issueform.partRecipientId" placeholder="请选择零件接收人" filterable @change="changeUser">
						<el-option v-for="item in userList" :key="item.userId" :label="item.nickName" :value="item.userId"> </el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="接收日期" prop="partRecipientDate">
					<el-date-picker v-model="issueform.partRecipientDate" type="date" value-format="yyyy-MM-dd" placeholder="请选择接收日期"> </el-date-picker>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button type="primary" @click="submitIssueForm">确 定</el-button>
				<el-button @click="issueCancel">取 消</el-button>
			</div>
		</el-dialog> -->
		<!-- 入库 -->
		<el-dialog title="修改状态" :visible.sync="issueopen" width="900px" append-to-body v-dialogDrag>
			<el-form ref="issueform" :model="issueform" :rules="issuerules" label-width="95px">
				<el-row :gutter="20">
					<el-col :span="12">
						<el-form-item label="状态" prop="submitStatus">
							<el-radio-group v-model="issueform.submitStatus">
								<el-radio label="2">确认</el-radio>
								<el-radio label="3">驳回</el-radio>
							</el-radio-group>
						</el-form-item>
					</el-col>
				</el-row>
				<el-row :gutter="20" v-if="issueform.submitStatus == '3'">
					<el-col :span="24">
						<el-form-item label="原因" prop="submitReason">
							<el-input v-model="issueform.submitReason" placeholder="请输入原因" type="textarea" :rows="3" maxlength="100" show-word-limit />
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button type="primary" @click="submitInForm">确 定</el-button>
				<el-button @click="issueopen = false">取 消</el-button>
			</div>
		</el-dialog>
	</div>
</template>

<script>
import { listEntrance, listEntranceChild, submitEntrance, delEntrance, handleEntranceIn, handleEntranceBack, handleEntranceDown, submitCert, submitIssue, productionApprove, getPlanData, editEntrancePlan } from "@/api/synthesize/outsource/oentrance"
import { getUserList, getcolumn } from "@/api/selectList"
import { checkout, CertificateUp, editsynthesize } from "@/api/quality/oentrance"
import CreateForm from "@/views/components/CreateForm"
import { mapGetters } from "vuex"
import FileUpload from "@/components/FileUpload"

export default {
	name: "CoutsourceFilling",
	dicts: ["default_status", "inspect_status", "inspection_method", "test_method", "default_approve", "product_intact_state", "supplier"],
	components: { CreateForm, FileUpload },
	computed: {
		...mapGetters(["sidebar"])
	},
	data() {
		return {
			tableTreeRefreshTool: {}, // 用于存储 resolve 方法和展开状态
			isMousedownTable: false, //是否按下表格
			currentEnterRow: null, //当前移入的行数据
			eleTabTop: 0, //表格容器距浏览器顶部距离
			testval: undefined,
			// 遮罩层
			loading: true,
			// 选中数组
			ids: [],
			rows: [],
			// 非单个禁用
			single: true,
			// 非多个禁用
			multiple: true,
			// 显示搜索条件
			showSearch: true,
			// 总条数
			total: 0,
			// 设备保养计划表格数据
			EntranceList: [],
			// 弹出层标题
			title: "",
			// 是否显示弹出层
			open: false,
			// 查询参数
			queryParams: {
				pageNum: 1,
				pageSize: 10,
				deviceName: undefined,
				registrationStatus: undefined,
				applyTime: undefined,
				partFigure: undefined,
				outsourcingDate: undefined,
				repairNumber: undefined,
				partRecipientName: undefined,
				partRecipientDate: undefined,
				serialNumber: undefined
			},
			// 表单参数
			form: {},
			// 表单校验
			rules: {
				inspectionMethod: [{ required: true, message: "请选择检验方式", trigger: "blur" }],
				testMethod: [{ required: true, message: "请选择检验方法", trigger: "blur" }],
				quantityquantity: [{ required: true, message: "输入数量", trigger: "blur" }],
				checkoutId: [{ required: true, message: "请选择检验人员", trigger: "blur" }]
			},
			certrules: {
				certificateRecipientDate: [{ required: true, message: "请输入合格证接收日期", trigger: "blur" }],
				certificateRecipientNumber: [{ required: true, message: "请输入合格证编号", trigger: "blur" }]
			},
			issuerules: {
				submitStatus: [{ required: true, message: "请选择状态", trigger: "blur" }]
				// partRecipientDate: [{ required: true, message: "请选择接收日期", trigger: "blur" }]
			},
			inrules: {
				deviceTypeId: [{ required: true, message: "请选择供方", trigger: "blur" }],
				recipientId: [{ required: true, message: "请选择接收人", trigger: "blur" }],
				recipientDate: [{ required: true, message: "请选择接收时间", trigger: "blur" }]
			},
			deviceList: [],
			// 更新树刷新
			treeMap: new Map(),
			// 审批数据
			openApprove: false,
			approveRow: undefined,
			// 生产计划
			isOne: false,
			disabled: false,
			// 审批流程
			openRecords: false,
			approveRecord: [],
			treetablekey: Math.random(),
			// 上传合格证
			certopen: false,
			certform: {},
			uploadList: [],
			// 下发
			issueopen: false,
			issueform: {},
			userList: [],
			// 下发
			inopen: false,
			inform: {},
			// 列信息
			columns: [
				// { key: 0, label: `记录编号`, visible: true },
				{ key: 0, label: `编号`, visible: true },
				{ key: 1, label: `外委日期`, visible: true },
				// { key: 3, label: `供方`, visible: true },
				// { key: 4, label: `送检时间`, visible: true },
				{ key: 2, label: `返修单号`, visible: true },
				// { key: 6, label: `生产批次`, visible: true },
				{ key: 3, label: `产品名称`, visible: true },
				{ key: 4, label: `产品型号`, visible: true },
				{ key: 5, label: `零件组名称`, visible: true },
				{ key: 6, label: `零件组图号`, visible: true },
				{ key: 7, label: `数量`, visible: true },
				// { key: 12, label: `接收人`, visible: true },
				// { key: 13, label: `发放人`, visible: true },
				// { key: 14, label: `发放日期`, visible: true },
				// { key: 15, label: `合格证接收人`, visible: true },
				// { key: 16, label: `接收日期`, visible: true },
				{ key: 8, label: `零件接收人`, visible: true },
				{ key: 9, label: `接收日期`, visible: true },
				{ key: 10, label: `状态`, visible: true },
				{ key: 11, label: `审核状态`, visible: true },
				{ key: 12, label: `缺件备注`, visible: true }
			]
		}
	},
	created() {
		this.getSize()
		this.getUserList()
	},
	mounted() {
		// 滚动条的获取
		this.handleScrollx()
		window.addEventListener("scroll", this.handleScrollx, true)
	},
	activated() {
		this.getSize()
		this.getUserList()
		//获取表格容器距页面头部的距离
		this.handleScrollx()
	},
	destroyed() {
		window.removeEventListener("scroll", this.handleScrollx, true)
	},
	methods: {
		// 关闭所有展开的行
		closeAllExpansions() {
			this.EntranceList.forEach(row => {
				this.$refs.multipleTable.toggleRowExpansion(row, false)
			})
      this.$refs.multipleTable.clearSelection()
	  this.getList()
		},

		getSize() {
			getcolumn({ pageName: "Pagination77" }).then(response => {
				this.queryParams.pageSize = response.data.length ? (response.data[0].limit ? response.data[0].limit : 10) : 10
				this.getList()
			})
		},
		handleSubmits() {
			let checkp = this.rows.every(item => {
				return typeof item.id == "number"
			})
			if (!checkp) {
				this.$modal.msg("只能选择父级产品进行提交")
				return false
			}
			const _this = this
			this.$modal
				.confirm(`是否确认提交数据？`)
				.then(function () {
					return editsynthesize({
						idList: _this.ids,
						submitStatus: "1"
					})
				})
				.then(() => {
					_this.getList()
					_this.$modal.msgSuccess("提交成功")
					_this.$refs.multipleTable.clearSelection()
				})
				.catch(() => {})
		},
		// 列表父级提交
		handleSubmit(row) {
			this.$modal
				.confirm("是否确认提交数据项？")
				.then(function () {
					let data = {
						id: row.id,
						// 1是提交，3是重复提交
						submitStatus: "1"
					}
					return editsynthesize(data)
				})
				.then(() => {
					this.getList()
					this.$modal.msgSuccess("操作成功")
          this.$refs.multipleTable.clearSelection()
				})
				.catch(() => {})
		},
		//获取表格容器距页面头部的距离
		handleScrollx() {
			this.eleTabTop = this.$refs.eleTab.getBoundingClientRect().top
		},
		// 获取用户下拉选
		getUserList() {
			getUserList().then(res => {
				this.userList = res.rows
			})
		},
		changeUser() {
			let cur = this.userList.find(item => {
				return item.userId == this.form.checkoutId
			})
			console.log(cur)
			this.form.checkoutName = cur.nickName
		},
		changeInUser() {
			let cur = this.userList.find(item => {
				return item.userId == this.inform.recipientId
			})
			this.inform.recipientName = cur.nickName
		},
		refreshLoadTree(parentId) {
			const { tree, treeNode, resolve } = this.treeMap.get(parentId)
			this.loadListDes(tree, treeNode, resolve) // 重新加载子级数据
      this.$refs.multipleTable.clearSelection()
		},
		/** 查询设备保养计划列表 */
		getList() {
			this.loading = true
			listEntrance(this.queryParams).then(response => {
				this.EntranceList = response.rows
				this.EntranceList.forEach((item, index) => {
					item.hasChildren = true
					item.sort = index + 1
					// this.$refs.multipleTable.toggleRowExpansion(item, false);
				})
				this.total = response.total
				this.loading = false
			})
		},
		// 展开/收起时触发
		handleExpandChange(row, expanded) {
			const curr = this.tableTreeRefreshTool[row.id]
			if (!curr) return
			curr.expandCount++
			if (expanded && curr.expandCount > 1 && !curr.prevStatus) {
				listEntranceChild({
					id: row.id,
					deviceName: this.queryParams.deviceName,
					registrationStatus: this.queryParams.registrationStatus,
					applyTime: this.queryParams.applyTime,
					partFigure: this.queryParams.partFigure,
					outsourcingDate: this.queryParams.outsourcingDate,
					repairNumber: this.queryParams.repairNumber,
					partRecipientName: this.queryParams.partRecipientName,
					partRecipientDate: this.queryParams.partRecipientDate
				}).then(res => {
					res.rows.forEach((item, index) => {
						// 父子id会冲突
						item.id = "c" + item.id
						item.sort = index + 1
					})
					curr.resolve(res.rows)
				})
			}
			curr.prevStatus = expanded
		},
		// 获取列表详情
		async loadListDes(tree, treeNode, resolve) {
			if (!this.treeMap.has(tree.id)) {
        this.treeMap.set(tree.id, { tree: tree, treeNode, resolve })
			}
      this.tableTreeRefreshTool[tree.id] = {
				resolve,
				expandCount: 0, // 展开次数
				prevStatus: false // 上一次的展开状态
			}
			let data = {
				id: tree.id,
				deviceName: this.queryParams.deviceName,
				registrationStatus: this.queryParams.registrationStatus,
				applyTime: this.queryParams.applyTime,
				partFigure: this.queryParams.partFigure,
				outsourcingDate: this.queryParams.outsourcingDate,
				repairNumber: this.queryParams.repairNumber,
				partRecipientName: this.queryParams.partRecipientName,
				partRecipientDate: this.queryParams.partRecipientDate
			}
			let ret = await listEntranceChild(data)
      tree.hasChildren = ret.rows.length > 0
			tree.children = ret.rows

      if (ret.rows.length == 0) {
				resolve([]) // 确保返回空数组
				this.$set(this.$refs.multipleTable.store.states.lazyTreeNodeMap, tree.id, [])
			} else {
				ret.rows.forEach((item, index) => {
					item.id = "c" + item.id
					item.sort = index + 1
				})
				resolve(ret.rows)
			}
		},
		// 取消按钮
		cancel() {
			this.open = false
			this.reset()
		},
		// 取消合格证上传
		certCancel() {
			this.certopen = false
			this.certReset()
		},
		// 取消下发
		issueCancel() {
			this.issueopen = false
			this.issueReset()
		},
		// 取消入库
		inCancel() {
			this.inopen = false
			this.inReset()
		},
		// 表单重置
		reset() {
			this.form = {
				claddingMaterial: "0",
				coating: "0",
				appearance: "0",
				specificationsModel: "0",
				quantity: "0",
				certificate: "0",
				screwThread: "0",
				inspectionMethod: undefined,
				testMethod: undefined,
				checkoutId: undefined,
				quantityquantity: undefined,
				disposalSituation: undefined,
				remark: undefined,
				repairNumber: undefined,
				partRecipientName: undefined,
				partRecipientDate: undefined,
				serialNumber: undefined
			}
			this.resetForm("form")
		},
		certReset() {
			this.certform = {
				ids: undefined,
				certificateRecipientNumber: undefined,
				certificateRecipientDate: undefined,
				fileList: []
			}
			this.resetForm("certform")
		},
		issueReset() {
			this.issueform = {
				id: undefined,
				submitStatus: undefined,
				submitReason: undefined
			}
			this.resetForm("issueform")
		},
		inReset() {
			this.inform = {
				ids: undefined,
				deviceTypeId: undefined,
				recipientId: undefined,
				recipientName: undefined,
				recipientDate: undefined
			}
			this.resetForm("inform")
		},
		/** 搜索按钮操作 */
		handleQuery() {
			this.queryParams.pageNum = 1
			this.getList()
			this.treetablekey = Math.random()
		},
		/** 重置按钮操作 */
		resetQuery() {
			this.resetForm("queryForm")
			this.handleQuery()
		},
		// 表格按下
		mousedownTable(event) {
			const target = event.target
			// 判断target的class属性确定是否点击在复选框上
			if (target.classList.contains("el-checkbox__inner")) {
				this.currentEnterRow && this.$refs.multipleTable.toggleRowSelection(this.currentEnterRow)
				this.isMousedownTable = true
			}
		},
		// 单击表格行
		row_click(row, column, event) {
			// this.currentEnterRow || this.$refs.table.toggleRowSelection(row)
			if (column && column.label == "操作") {
				return
			} else {
				this.$refs.multipleTable.toggleRowSelection(row)
			}
		},
		changeTableData() {
			this.$nextTick(() => {
				this.$refs.multipleTable.doLayout()
			})
		},

		// 进入单元格
		cell_mouse_enter(row, column, cell, event) {
			this.isMousedownTable && this.$refs.multipleTable.toggleRowSelection(row)
			this.currentEnterRow = row
		},
		// 离开单元格
		cell_mouse_leave(row, column, cell, event) {
			this.currentEnterRow = null
		},
		// 鼠标弹起或者离开表格
		mouseupTable(d) {
			this.isMousedownTable = false
		},
		// 表格每行样式
		row_class_name({ row, rowIndex }) {
			return this.rows.find(v => v.id == row.id) ? "selected" : ""
		},
		// 多选框选中数据
		handleSelectionChange(selection) {
			this.ids = selection.map(item => item.id)
			this.rows = selection
			this.single = selection.length !== 1
			this.multiple = !selection.length
		},
		checkIds() {
			// 判断ids
			if (this.ids.length == 0) {
				this.$modal.msg("请选择产品")
				return false
			} else {
				// 判断是否选中了父级返修任务
				let checkp = this.rows.find(item => {
					return Number(item.id) > 0
				})
				if (checkp) {
					this.$modal.msg("只能选择子类产品")
					return false
				}
			}
			let ids = this.ids.map(item => {
				return item.replace("c", "")
			})
			return ids
		},
		/** 送检操作 */
		handleAdd() {
			let ids = this.checkIds()
			if (!ids) {
				return false
			}
			this.reset()
			this.form.ids = ids
			this.open = true
			this.title = "检验"
		},
		handleCert() {
			let ids = this.checkIds()
			if (!ids) {
				return false
			}
			this.reset()
			this.certform.ids = ids
			this.certopen = true
		},
		// 入库
		handleIn() {
			let ids = this.checkIds()
			if (!ids) {
				return false
			}
			// 判断状态
			let checkrows = false
			this.rows.forEach(item => {
				if (item.registrationStatus != "0" && item.registrationStatus != "2") {
					checkrows = true
				}
			})
			if (checkrows) {
				this.$modal.msg("只有待入库和退货可以入库")
				return false
			}
			this.reset()
			this.inform.ids = ids
			this.inopen = true
		},
		/** 提交按钮 */
		submitForm(val) {
			this.$refs["form"].validate(valid => {
				if (valid) {
					this.form.ids = this.checkIds()
					this.form.registrationStatus = val
					checkout(this.form).then(response => {
						this.$modal.msgSuccess("操作成功")
						this.open = false
						this.getList()
            this.$refs.multipleTable.clearSelection()
						this.treetablekey = Math.random()
					})
				}
			})
		},
		// 提交上传合格证
		submitCertForm() {
			this.$refs["certform"].validate(valid => {
				if (valid) {
					if (this.uploadList.length == 0) {
						this.$modal.msgError("请先上传文件")
						return false
					}
					this.certform.fileList = this.uploadList
					CertificateUp(this.certform).then(response => {
						this.$modal.msgSuccess("操作成功")
						this.certopen = false
            this.$refs.multipleTable.clearSelection()
						this.treetablekey = Math.random()
					})
				}
			})
		},
		// 下发
		submitIssueForm() {
			this.$refs["issueform"].validate(valid => {
				if (valid) {
					submitIssue(this.issueform).then(response => {
						this.$modal.msgSuccess("操作成功")
						this.issueopen = false
            this.$refs.multipleTable.clearSelection()
						this.treetablekey = Math.random()
					})
				}
			})
		},
		// 入库
		submitInForm() {
			this.$refs["issueform"].validate(valid => {
				if (valid) {
					editsynthesize(this.issueform).then(response => {
						this.$modal.msgSuccess("操作成功")
						this.issueopen = false
            this.$refs.multipleTable.clearSelection()
						this.getList()
					})
				}
			})
		},
		handleEdit(row) {
			this.issueReset()
			this.issueform.id = row.id
			this.issueopen = true
		},
		// 删除
		handleDelete(row) {
			const ids = row.id || this.ids
			this.$modal
				.confirm("是否确认删除数据项？")
				.then(function () {
					return delEntrance(ids)
				})
				.then(() => {
					this.$modal.msgSuccess("删除成功")
					this.getList()
          this.$refs.multipleTable.clearSelection()
					this.treetablekey = Math.random()
				})
				.catch(() => {})
		},
		// 关闭更改保养计划
		cancelChange() {
			this.changeOpen = false
		},
		// 工艺流程列表
		showTechnologyList() {
			return false
			alert("工艺流程列表")
		},
		// 附件
		handleAnnex(row) {
			this.$router.push({
				path: "/attachment/attachmentUpload",
				query: {
					TableId: row.id,
					typeId: 37,
					perm: "quality:customer:coutsource:annex"
				}
			})
		},
		handleAnnex2(row) {
			let id = row.id.replace("c", "")
			this.$router.push({
				path: "/attachment/attachmentUpload",
				query: {
					TableId: id,
					typeId: 38,
					perm: "quality:customer:coutsource:annex2"
				}
			})
		},

		// 上传
		getFiles(files) {
			this.uploadList = files
		}
	}
}
</script>

<style lang="scss" scoped>
.add-mid-title {
	width: 100%;
	margin: 20px 0 10px 0;
}
</style>
